home *** CD-ROM | disk | FTP | other *** search
/ C & C++ Multimedia Cyber Classroom / C and C++ Multimedia Cyber Classroom (Prentice Hall) (1998).iso / src / fig04_14.jar / Ch04 / Fig04_14 / Fig04_14.cpp
C/C++ Source or Header  |  1997-10-13  |  1KB  |  53 lines

  1. // Fig. 4.14: fig04_14.cpp
  2. // Passing arrays and individual array elements to functions
  3. #include <iostream.h>
  4. #include <iomanip.h>
  5.  
  6. void modifyArray( int [], int );  // appears strange
  7. void modifyElement( int );
  8.  
  9. int main()
  10. {
  11.    const int arraySize = 5;
  12.    int i, a[ arraySize ] = { 0, 1, 2, 3, 4 };
  13.  
  14.    cout << "Effects of passing entire array call-by-reference:" 
  15.         << "\n\nThe values of the original array are:\n";
  16.  
  17.    for ( i = 0; i < arraySize; i++ )
  18.       cout << setw( 3 ) << a[ i ];
  19.  
  20.    cout << endl;
  21.  
  22.    // array a passed call-by-reference
  23.    modifyArray( a, arraySize );  
  24.  
  25.    cout << "The values of the modified array are:\n";
  26.  
  27.    for ( i = 0; i < arraySize; i++ )
  28.       cout << setw( 3 ) << a[ i ];
  29.  
  30.    cout << "\n\n\n"
  31.         << "Effects of passing array element call-by-value:"
  32.         << "\n\nThe value of a[3] is " << a[ 3 ] << '\n';
  33.  
  34.    modifyElement( a[ 3 ] );
  35.  
  36.    cout << "The value of a[3] is " << a[ 3 ] << endl;
  37.  
  38.    return 0;
  39. }
  40.  
  41. void modifyArray( int b[], int sizeOfArray )
  42. {
  43.    for ( int j = 0; j < sizeOfArray; j++ )
  44.       b[ j ] *= 2;
  45. }
  46.  
  47. void modifyElement( int e )
  48. {
  49.    cout << "Value in modifyElement is " 
  50.         << ( e *= 2 ) << endl;
  51. }
  52.  
  53.